package com.rainbow.execl.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author rainbow
 **/
public class POIExcelUtil {


    public static final String FILE_EXTENSION_XLS = "xls";
    public static final String FILE_EXTENSION_XLSX = "xlsx";

    /**
     *
     * @param maps
     *            <String,String> maps 属性表，成员属性age为KEY，中文名称为VALUE
     * @param list
     *            <T> list 需要导出的数据列表对象
     * @param file
     *            file 指定输出文件位置，只能导出excel2003以上版本
     *
     * @return true 导出成功 false 导出失败
     */
    public static <T> boolean excelExport(Map<String, String> maps, List<T> list, File file) {

        try {
            Workbook wb = null;
            String filename = file.getName();
            String type = filename.substring(filename.lastIndexOf(".")+1).toLowerCase();
            if (type.equals(FILE_EXTENSION_XLS)) {
                wb = new HSSFWorkbook();
            }
            if (type.equals(FILE_EXTENSION_XLSX)) {
                wb = new XSSFWorkbook();
            }
            CreationHelper createHelper = wb.getCreationHelper();
            Sheet sheet = wb.createSheet("sheet1");
            Set<String> sets = maps.keySet();
            Row row = sheet.createRow(0);
            int i = 0;
            // 定义表头
            for (Iterator<String> it = sets.iterator(); it.hasNext();) {
                String key = it.next();
                Cell cell = row.createCell(i++);
                cell.setCellValue(createHelper.createRichTextString(maps.get(key)));
            }
            // 填充表单内容
            System.out.println("--------------------100%");
            float avg = list.size() / 20f;
            int count = 1;
            for (int j = 0; j < list.size(); j++) {
                T p = list.get(j);
                Class classType = p.getClass();
                int index = 0;
                Row row1 = sheet.createRow(j+1);
                for (Iterator<String> it = sets.iterator(); it.hasNext();) {
                    String key = it.next();
                    String firstLetter = key.substring(0, 1).toUpperCase();
                    // 获得和属性对应的getXXX()方法的名字
                    String getMethodName = "get" + firstLetter+ key.substring(1);
                    // 获得和属性对应的getXXX()方法
                    Method getMethod = classType.getMethod(getMethodName,new Class[] {});
                    // 调用原对象的getXXX()方法
                    Object value = getMethod.invoke(p, new Object[] {});
                    Cell cell = row1.createCell(index++);
                    cell.setCellValue(value.toString());
                }
                if (j > avg * count) {
                    count++;
                    System.out.print("I");
                }
                if (count == 20) {
                    System.out.print("I100%");
                    count++;
                }
            }
            FileOutputStream fileOut = new FileOutputStream(file);
            wb.write(fileOut);
            fileOut.close();

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (SecurityException e) {
            e.printStackTrace();
            return false;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return false;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return false;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
